/*
 * TabComponent.java
 *
 * Created on 13. Februar 2008, 16:53
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package de.blitzcoder.collide.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import javax.swing.plaf.basic.BasicButtonUI;
import java.util.*;
import de.blitzcoder.collide.icons.Icon;

/**
 *
 * @author blitzcoder
 * This is the component for the tabs. It Provides the name, and an X-Button etc.
 */
public class DocumentTabComponent extends JPanel implements MouseListener {

    private JLabel titleLabel;
    private JButton closeBtn;
    private String title;
    private LinkedList<ActionListener> closeListenerList = new LinkedList<ActionListener>();
    private TabPopupMenu popupMenu;
    
    private JTabbedPane tabber;
    private int tabIndex;

    /** Creates a new instance of TabComponent */
    public DocumentTabComponent(String title,javax.swing.Icon icon,JTabbedPane tabber,int tabIndex) {


        super(new FlowLayout(FlowLayout.LEFT, 0, 0));

        this.tabber = tabber;
        this.tabIndex = tabIndex;
        
        this.setOpaque(false);

        popupMenu = new TabPopupMenu();

        this.title = title;
        titleLabel = new JLabel(title + " ");
        closeBtn = new TabCloseButton();

        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));

        closeBtn.setPreferredSize(new Dimension(10, 10));

        titleLabel.setOpaque(false);
        if (icon != null)
            titleLabel.setIcon(icon);


        this.add(titleLabel);
        this.add(closeBtn);

        addMouseListener(this);

        closeBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ListIterator it = closeListenerList.listIterator();
                while (it.hasNext()) {
                    ((ActionListener) it.next()).actionPerformed(new ActionEvent(new Object(), 0, "Close-Button Clicked"));
                }
            }
        });


    }

    public void setTitle(String title) {
        titleLabel.setText(title);
    }

    public void addCloseListener(ActionListener cl) {
        closeListenerList.addLast(cl);
        popupMenu.addCloseListener(cl);
    }

    public void addCloseAllListener(ActionListener cl) {
        popupMenu.addCloseAllListener(cl);
    }

    public void addCloseOtherListener(ActionListener cl) {
        popupMenu.addCloseOtherListener(cl);
    }

    //public void addPropertiesListener(ActionListener cl) {
    //    popupMenu.addPropertiesListener(cl);
    //}

    public TabPopupMenu getPopupMenu() {
        return popupMenu;
    }

    static class TabButton extends JButton {

        public TabButton() {
            super();
            setUI(new BasicButtonUI());
            //Make it transparent
            setContentAreaFilled(false);
            //No need to be focusable
            //setFocusable(false);
            setBorderPainted(false);
            setRolloverEnabled(true);

        }
    }

    
    
    static class TabCloseButton extends TabButton {

        private static ImageIcon icon_close = Icon.load("closetab.png");
        private static ImageIcon icon_close_mouseover = Icon.load("closetab_mouseover.png");

        public TabCloseButton() {
            super();
            setIcon(icon_close);

            this.addMouseListener(new MouseAdapter() {

                public void mouseEntered(MouseEvent e) {
                    setIcon(icon_close_mouseover);
                }

                public void mouseExited(MouseEvent e) {
                    setIcon(icon_close);
                }
            });
        }
    }

    // Dirty workaround
    // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6180941
    public void mouseReleased(MouseEvent e) {
        if (e.getButton() == 3)
            popupMenu.show( e.getComponent(), e.getX(), e.getY() ); 
    }
    
    public void mouseClicked(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {
        if (e.getButton() != 3)
            tabber.setSelectedIndex(tabIndex);
    }
    public void mouseEntered(MouseEvent e) {
        e.translatePoint(getLocation().x, getLocation().y);
        tabber.dispatchEvent(e);
    }
    public void mouseExited(MouseEvent e) {
        e.translatePoint(getLocation().x, getLocation().y);
        tabber.dispatchEvent(e);
    }

}
